package com.xyt.imei.core.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luminary.common.framework.core.auth.ValidateToken;
import com.luminary.common.framework.core.model.Pagination;
import com.luminary.common.framework.core.model.SimpleResponse;
import com.luminary.component.util.common.BeanMapUtils;
import com.xyt.imei.core.meta.PageConstants;
import com.xyt.imei.core.meta.RoleEnum;
import com.xyt.imei.core.model.dto.WaitReciveQueryDTO;
import com.xyt.imei.core.model.dto.WaitReciveUndoDTO;
import com.xyt.imei.core.model.po.Store;
import com.xyt.imei.core.model.po.User;
import com.xyt.imei.core.service.StoreService;
import com.xyt.imei.core.util.TokenUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("waitRecive")
public class WaitReciveApi {

	@Autowired
	private TokenUtil tokenUtil;
	
	@Autowired
	private StoreService storeService;
	
	@ValidateToken(roles = {RoleEnum.ADMIN_VALUE + "", RoleEnum.BRANCH_VALUE + ""})
	@GetMapping("/from/{targetId}")
	public SimpleResponse queryFrom(
			@PathVariable(value = "targetId", required = false) int targetId,
			@PageableDefault(page=PageConstants.DEFAULT_PAGE_NO,size=PageConstants.DEFAULT_PAGE_SIZE)
			@SortDefault.SortDefaults({
				@SortDefault(sort = "create_date", direction = Direction.DESC)
			}) Pageable pageable,
			HttpServletResponse response
		) throws Exception {
		
		log.info("targetId:" + targetId + ",pageable:" + pageable.toString());
		
		User user = tokenUtil.getUser();
		
		Pagination<Store> pagination = new Pagination<Store>(pageable);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("targetUserId", targetId);
		params.put("userId", user.getUserId());
		pagination.setParams(params, pageable);
		
		return SimpleResponse.success(storeService.findWaitRecivePageBySource(pagination), response);
		
	}
	
	@ValidateToken(roles = {RoleEnum.ADMIN_VALUE + "", RoleEnum.BRANCH_VALUE + ""})
	@GetMapping("/to/{targetId}")
	public SimpleResponse queryTo(
			@PathVariable("targetId") int targetId,
			@Valid WaitReciveQueryDTO waitReciveQueryDTO,
			@PageableDefault(page=PageConstants.DEFAULT_PAGE_NO,size=PageConstants.DEFAULT_PAGE_SIZE)
			@SortDefault.SortDefaults({
				@SortDefault(sort = "create_date", direction = Direction.DESC)
			}) Pageable pageable,
			HttpServletResponse response
		) throws Exception {
		
		log.info("targetId:" + targetId + ",pageable:" + pageable.toString());
		
		Pagination<Store> pagination = new Pagination<Store>(pageable);
		Map<String, Object> params = new HashMap<String, Object>();
		params = BeanMapUtils.beanToMap(waitReciveQueryDTO);
		params.put("targetUserId", targetId);
		pagination.setParams(params, pageable);
		
		return SimpleResponse.success(storeService.findWaitRecivePageByTarget(pagination), response);
		
	}
	
	@ValidateToken(roles = {RoleEnum.ADMIN_VALUE + "", RoleEnum.BRANCH_VALUE + ""})
	@GetMapping("/self/to")
	public SimpleResponse queryToSelf(
			@Valid WaitReciveQueryDTO waitReciveQueryDTO,
			@PageableDefault(page=PageConstants.DEFAULT_PAGE_NO,size=PageConstants.DEFAULT_PAGE_SIZE)
			@SortDefault.SortDefaults({
				@SortDefault(sort = "create_date", direction = Direction.DESC)
			}) Pageable pageable,
			HttpServletResponse response
		) throws Exception {
		
		log.info("pageable:" + pageable.toString());
		
		User user = tokenUtil.getUser();
		
		Pagination<Store> pagination = new Pagination<Store>(pageable);
		Map<String, Object> params = new HashMap<String, Object>();
		params = BeanMapUtils.beanToMap(waitReciveQueryDTO);
		params.put("targetUserId", user.getUserId());
		pagination.setParams(params, pageable);
		
		return SimpleResponse.success(storeService.findWaitRecivePageByTarget(pagination), response);
		
	}
	
	@ValidateToken(roles = {RoleEnum.ADMIN_VALUE + "", RoleEnum.BRANCH_VALUE + ""})
	@PutMapping("/undo")
	public SimpleResponse undo(
			@RequestBody @Valid List<WaitReciveUndoDTO> waitReciveUndoDTO, 
			HttpServletResponse response) throws Exception {
		
		User user = tokenUtil.getUser();
		
		if(!storeService.undo(waitReciveUndoDTO, user))
			return SimpleResponse.failed("撤销失败", response);
		
		return SimpleResponse.success("撤销成功", response);
		
	}
	
	@ValidateToken(roles = {RoleEnum.ADMIN_VALUE + "", RoleEnum.BRANCH_VALUE + ""})
	@PutMapping("/recive")
	public SimpleResponse recive(
			@RequestBody @Valid List<WaitReciveUndoDTO> waitReciveUndoDTO,
			HttpServletResponse response) throws Exception {
		
		User user = tokenUtil.getUser();
		
		if(!storeService.recive(waitReciveUndoDTO, user))
			return SimpleResponse.failed("接收失败", response);
		
		return SimpleResponse.success("接收成功", response);
		
	}
	
}
